package com.hcbd.tdwt.base;

import android.util.Log;

import com.hcbd.tdwt.util.ConvertData;
import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.util.FileUtil;
import com.hcbd.tdwt.util.FileUtils;
import com.hcbd.tdwt.util.StringUtil;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.DisasterEarlyWarning;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.RealTimeWeather;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherForecast;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherInfo;
import com.hcbd.tdwt.ui.fuda.common.vo.weather.WeatherModeType;
import com.google.gson.Gson;
import com.pancoit.compression.ZDCompression;
import com.hcbd.tdwt.MainApp;

import com.hcbd.tdwt.ui.xingyu.vo.BoxContact;
import com.hcbd.tdwt.ui.xingyu.vo.DeviceTrackVO;
import com.hcbd.tdwt.ui.xingyu.vo.UserMessageVO;
import com.pangu.bdsdk2021.entity.terminalone.BDTXInfo;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ConvertToUserMsgTool {

     public static  int msg_type=1;
    /**
     * 北斗网通信
     * 0xA4
     *
     * @param bdtxInfo
     * @return
     */
    public static UserMessageVO cast0xA4ToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头,保留字段5位直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        //内容
        String content = ConvertData.hexToString(hexData, ConvertData.GB18030);
        String lon=null;
        String lat=null;
        String newContent=content;
        int index=content.indexOf("(POS:");
        try {
            if (index != -1) {
                newContent = content.substring(0, index);
                String loc = content.substring(index + 5, content.length() - 1);
                String[] locArray = loc.split(",");
                lon = locArray[0];
                lat = locArray[1];
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(newContent);
        userMessage.setSendTime(DateUtils.getDateTime());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setMsgType(Constant.TEXT);
        userMessage.setRead(false);
        userMessage.setLongitude(lon);
        userMessage.setLatitude(lat);
        return userMessage;
    }
    /**
     * 北斗网通信
     * 汉字模式
     *
     * @param bdtxInfo
     * @return
     */
    public static UserMessageVO castChineToUserMessage(BDTXInfo bdtxInfo) {
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(bdtxInfo.content);
        userMessage.setSendTime(DateUtils.getDateTime());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setMsgType(Constant.TEXT);
        userMessage.setRead(false);
        return userMessage;
    }
    /**
     * 北斗网到北斗网消息(不带位置)
     * 0x90
     *
     * @param bdtxInfo
     */
    public static UserMessageVO cast0x90ToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头,保留字段5位直接跳过不解析
        String hexData = bdtxInfo.content.substring(12);
        //时间
        long time = ConvertData.hexToLong(hexData.substring(0, 8));
        //内容
        String content = ConvertData.hexToString(hexData.substring(8), ConvertData.GB18030);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(content);

//        userMessage.setSendTime(DateUtils.timeStampToDate(String.valueOf(time), null));
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        //userMessage.setMessageType("消息来自北斗网");
        userMessage.setRead(false);
        userMessage.setMsgType(Constant.TEXT);
        return userMessage;
    }
    /**
     * 转换收到的消息(天地卫通协议)
     *
     * @param bdtxInfo
     */
    public static UserMessageVO convertMessage(BDTXInfo bdtxInfo) {
        if(msg_type==bdtxInfo.messageType){//如果是汉字模式
            return castChineToUserMessage(bdtxInfo);
        }
        String headerStr = bdtxInfo.content.substring(0, 2);
        switch (headerStr) {
            case "90"://北斗网到北斗网(不带位置)
                return cast0x90ToUserMessage(bdtxInfo);
            case "91"://返回紧急联系人(带位置)
                return  cast0x91ToUserMessage(bdtxInfo);
            case "A7"://人对人语音
                return cast0xA7ToUserMessage(bdtxInfo);
            case "A9"://图片
                return cast0xA9ToUserMessage(bdtxInfo.content,String.valueOf(bdtxInfo.fromNumber));
            case "A4":
                return cast0xA4ToUserMessage(bdtxInfo);
            case "14":
                return cast0x14ToUserMessage(bdtxInfo.content,String.valueOf(bdtxInfo.fromNumber));
            case "01"://SOS上报数据
                return cast0x01ToUserMessage(bdtxInfo.content,String.valueOf(bdtxInfo.fromNumber));
            case "00"://位置上报数据
                return cast0x00A4ToUserMessage(bdtxInfo.content,String.valueOf(bdtxInfo.fromNumber));
            case "58"://天气数据
                return cast0x58ToUserMessage(bdtxInfo);
            case "56"://天气数据
                return cast0x56ToUserMessage(bdtxInfo);
            case "59"://天气数据
                return cast0x59ToUserMessage(bdtxInfo);
            case "5A":
                return cast0x5AToUserMessage(bdtxInfo);
            case "A1":
                return cast0xA1ToUserMessage(bdtxInfo);
            case "A0":
                return cast0xA0ToUserMessage(bdtxInfo);
            case "13":
            case "93":
            case "95":
            case "15":
                return null;
            default:
                // 其他直接接收
//                MainApp.getInstance().showMsg("收到其他消息：" + bdtxInfo.content);
//                return null;
                return castChineToUserMessage(bdtxInfo);
        }

    }
    /**
     * 北斗网到北斗网消息(带位置)
     * 0x91
     *
     * @param bdtxInfo
     */
    public static UserMessageVO cast0x91ToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头,保留字段5位直接跳过不解析
        String hexData = bdtxInfo.content.substring(12);
        //时间
        long time = ConvertData.hexToLong(hexData.substring(0, 8));
        //经度
        long longitude = ConvertData.hexToLong(hexData.substring(8, 16));
        //纬度
        long latitud = ConvertData.hexToLong(hexData.substring(16, 24));
        //高度
        long altitude = ConvertData.hexToLong(hexData.substring(24, 28));
        //内容
        String content = ConvertData.hexToString(hexData.substring(28), ConvertData.GB18030);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(content);
//        userMessage.setSendTime(DateUtils.timeStampToDate(String.valueOf(time), null));
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        if(longitude==0){
            userMessage.setLongitude("0");
        }else{
            userMessage.setLongitude(StringUtil.stringInsert(String.valueOf(longitude), ".", 3));
        }
        if(latitud==0){
            userMessage.setLatitude("0");
        } else{
            userMessage.setLatitude(StringUtil.stringInsert(String.valueOf(latitud), ".", 2));
        }
        userMessage.setAltitude(String.valueOf(altitude));
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        //userMessage.setMessageType("消息来自北斗网");
        userMessage.setMsgType(Constant.TEXT);
        userMessage.setRead(false);
        return userMessage;
    }
    /***
     * 接收语音
     * @param bdtxInfo
     * @return
     */
    public static UserMessageVO cast0xA8ToUserMessage(BDTXInfo bdtxInfo) {
        if(bdtxInfo==null){
            return null;
        }
        //第1位请求头直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        String contentStr=hexData.substring(10);
        byte[] content =ConvertData.hex2bytes(contentStr);
        FileOutputStream out=null;
        String audioPcmFilePath= FileUtil.getAudioAccFile()+DateUtils.getDateShortSerial() +".pcm";
        File aacFile = new File(audioPcmFilePath);
        try {
            if (!aacFile.exists()) {
                aacFile.createNewFile();
            }
            Log.e("接收压缩数据：",""+content.length);
            byte[] unZipContent= ZDCompression.getInstance().unZip(content);
            out=new FileOutputStream(aacFile);
            out.write(unZipContent);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        int seconds= FileUtils.getPcmSeconds(audioPcmFilePath);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setMsgType(Constant.AUDIO);
        userMessage.setSeconds(seconds);
        userMessage.setContent(audioPcmFilePath);
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setRead(false);
        return userMessage;
    }
    /***
     * 接收语音A7
     * @param bdtxInfo
     * @return
     */
    public static UserMessageVO cast0xA7ToUserMessage(BDTXInfo bdtxInfo) {
        if(bdtxInfo==null){
            return null;
        }
        //第1位请求头直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        String contentStr=hexData.substring(10);
        byte[] content =ConvertData.hex2bytes(contentStr);
        FileOutputStream out=null;
        String audioPcmFilePath= FileUtil.getAudioAccFile()+DateUtils.getDateShortSerial() +".pcm";
        File aacFile = new File(audioPcmFilePath);
        try {
            if (!aacFile.exists()) {
                aacFile.createNewFile();
            }
            Log.e("接收压缩数据：",""+content.length);
            byte[] unZipContent= ZDCompression.getInstance().unZip(content);
            out=new FileOutputStream(aacFile);
            out.write(unZipContent);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        int seconds= FileUtils.getPcmSeconds(audioPcmFilePath);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setMsgType(Constant.AUDIO);
        userMessage.setSeconds(seconds);
        userMessage.setContent(audioPcmFilePath);
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setRead(false);
        return userMessage;
    }
    public static UserMessageVO cast0xA9ToUserMessage(String hex,String fromNumber) {

        //第1位请求头直接跳过不解析
        String hexData = hex.substring(2);
        //发送者号码
        long fromPhone = ConvertData.hexToLong(hexData.substring(0, 10));
        //时间
        long time = ConvertData.hexToLong(hexData.substring(10, 18));
        // 总包数量
        int total = ConvertData.hexToInt(hexData.substring(18, 20));
        Log.e("总包数量",total+"");
        //包序号
        int id=ConvertData.hexToInt(hexData.substring(20, 22));
        Log.e("包序号",id+"");
        String hexContent=hexData.substring(22);
        byte[] content =ConvertData.hex2bytes(hexContent);
        String uniqueSerialNumber="A9"+time+fromNumber;
        Log.e("uniqueSerialNumber：",uniqueSerialNumber);
        ReceiveImgManager receiveImgData=ReceiveImgManager.getReceiveImg(id,uniqueSerialNumber,content);
        try {
            ZDCompression.getInstance().decodeJ2kImg(receiveImgData.getListByte(),receiveImgData.getPath(),1,false);
        }catch (Exception e){
            e.printStackTrace();
        }
        ReceiveImgManager.startTime();//开启接收定时器
        UserMessageVO userMsg=ReceiveImgManager.getUserMsg(receiveImgData,String.valueOf(fromNumber),total,id,time);
        EventBus.getDefault().post(userMsg);
        //sendMsg("onMessage");
        return null;
    }
    /**
     * 报平安
     * 0x14
     *
     * @param hex
     * @param fromNumber
     */
    public static UserMessageVO cast0x14ToUserMessage(String hex,String fromNumber) {
        //第1位请求头,保留字段3,手机号3位直接跳过不解析
        String hexData = hex.substring(12);
        //经度
        long longitude = ConvertData.hexToLong(hexData.substring(0, 8));
        //纬度
        long latitud = ConvertData.hexToLong(hexData.substring(8, 16));
        //高度
        long altitude = ConvertData.hexToLong(hexData.substring(16, 20));
        //内容
        String content = ConvertData.hexToString(hexData.substring(20), ConvertData.GB18030);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(fromNumber);
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(content);
        userMessage.setLongitude(StringUtil.stringInsert(String.valueOf(longitude), ".", 3));
        userMessage.setLatitude(StringUtil.stringInsert(String.valueOf(latitud), ".", 2));
        userMessage.setAltitude(String.valueOf(altitude));
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setRead(false);
        userMessage.setMsgType(Constant.TEXT);
        return userMessage;
    }
    /**
     * 自动位置报告-定位方式3
     * 0x00A4
     *
     * @param hex
     * @param fromNumber
     */
    public static UserMessageVO cast0x00A4ToUserMessage(String hex,String fromNumber) {
        //第2位请求头,保留字段3,手机号3位直接跳过不解析
        String hexData = hex.substring(4);
        //时间
        long time = ConvertData.hexToLong(hexData.substring(0, 12));
        //采样频度
        int sampling = ConvertData.hexToInt(hexData.substring(12, 16));
        //本包信息个数
        int infoNum = ConvertData.hexToInt(hexData.substring(16, 18));
        //内容
        String content = hexData.substring(18);

        DeviceTrackVO currentTrackVO=infoContent(fromNumber,content,infoNum);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(fromNumber);
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(content);
        if(currentTrackVO!=null){
            userMessage.setLongitude(StringUtil.changeToStr(currentTrackVO.longitude));
            userMessage.setLatitude(StringUtil.changeToStr(currentTrackVO.latitude));
        }else{
            userMessage.setLongitude("0");
            userMessage.setLatitude("0");
        }
        userMessage.setAltitude("0");
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setRead(false);
        userMessage.setMsgType(Constant.TEXT);

        BoxContact.cacheFromBoxId(String.valueOf(fromNumber),userMessage.getMsgType(),null,userMessage.getLongitude(),userMessage.getLatitude(),true);
        EventBus.getDefault().post(userMessage);
        return null;
    }
    /**
     * 公网到北斗网(不带位置)
     * 0xA0
     *

     * @return
     */
    public static UserMessageVO cast0xA0ToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        //发送者号码
        long fromPhone = ConvertData.hexToLong(hexData.substring(0, 10));
        //时间
        long time = ConvertData.hexToLong(hexData.substring(10, 18));
        //内容
        String conten = ConvertData.hexToString(hexData.substring(18), ConvertData.GB18030);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(conten);
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
       // userMessage.setMessageType("消息来自公网");
        userMessage.setRead(false);
        return userMessage;
    }

//    public static UserMessageVO castCodeToUserMessage(BDTXInfo bdtxInfo) {
//        //返回消息
//        UserMessageVO userMessage = new UserMessageVO();
//        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
//        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
//        userMessage.setContent(bdtxInfo.content);
//        userMessage.setSendTime(DateUtils.getDateTime());
//        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
//        userMessage.setMsgType(Constant.TEXT);
//        userMessage.setRead(false);
//        return userMessage;
//    }

    /**
     * 公网到北斗网(带位置)
     * 0xA1
     * @return
     */
    public static UserMessageVO  cast0xA1ToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        //发送者号码
        long fromPhone = ConvertData.hexToLong(hexData.substring(0, 10));
        //时间
        long time = ConvertData.hexToLong(hexData.substring(10, 18));
        //经度
        long longitude = ConvertData.hexToLong(hexData.substring(18, 26));
        //纬度
        long latitud = ConvertData.hexToLong(hexData.substring(26, 34));
        //高度
        long altitude = ConvertData.hexToLong(hexData.substring(34, 38));
        //内容
        String content = ConvertData.hexToString(hexData.substring(38), ConvertData.GB18030);
        //返回消息
        UserMessageVO userMessage = new UserMessageVO();
        userMessage.setReceiverNumber(String.valueOf(bdtxInfo.fromNumber));
        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
        userMessage.setContent(content);
        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setLongitude(StringUtil.stringInsert(String.valueOf(longitude), ".", 3));
        userMessage.setLatitude(StringUtil.stringInsert(String.valueOf(latitud), ".", 2));
        userMessage.setAltitude(String.valueOf(altitude));
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setRead(false);
        return userMessage;
    }
    /**
     * SOS位置上报接收01
     * 0x01
     *
     * @param
     * @param fromNumber
     */
    public static UserMessageVO cast0x01ToUserMessage(String hex,String fromNumber) {
        //第4位请求头01EE
        String hexData = hex.substring(4);
        //上报时间时间
        String timeStr=hexData.substring(0, 12);
        long time=ConvertData.hexToLong(timeStr);

        //经度指示45/E 57/W
        String longitudeIndication=hexData.substring(12,14);
        //经度
        String hex_14_22= hexData.substring(14,22);
        String longitude = ConvertData.hexToDegMinSeconds(hex_14_22);
        //纬度指示 4E/N 53/S
        String hex_22_24= hexData.substring(22,24);

        //纬度
        String hex_24_32= hexData.substring(24,32);
        String latitude= ConvertData.hexToDegMinSeconds(hex_24_32);


        UserMessageVO userMessage=new UserMessageVO();

        userMessage.setMsgType(Constant.TEXT);

        userMessage.setContent("SOS位置数据上报数据");

        userMessage.setReceiverNumber(fromNumber);

        userMessage.setSendNumber(BeidouBoxParams.userCardNumber);

        userMessage.setLongitude(StringUtil.changeToStr(longitude));
        userMessage.setLatitude(StringUtil.changeToStr(latitude));
        //userMessage.setAltitude(String.valueOf(altitude));

        userMessage.setSendTime(DateUtils.getDateLongSerial());
        userMessage.setIoType(Constant.RECEIVE_MESSAGE);
        userMessage.setRead(false);
        BoxContact.cacheFromBoxId(String.valueOf(fromNumber),userMessage.getMsgType(),null,userMessage.getLongitude(),userMessage.getLatitude(),true);
        EventBus.getDefault().post(userMessage);
        return null;
    }




    public static DeviceTrackVO infoContent(String fromNumber,String content,int infoNum){
        DeviceTrackVO currentTrackVO=null;
        for(int i=0;i<infoNum;i++){
            if(content.length()>30){
                DeviceTrackVO deviceTrackVO=new DeviceTrackVO();
                deviceTrackVO.number=fromNumber;
                String info=content.substring(i*30,(i+1)*30);
                //经度指示
                String longitudeIndication=info.substring(0,2);
                //经度
                String hex_2_10= info.substring(2, 10);

                String longitude = ConvertData.hexToDegMinSeconds(hex_2_10);
                //纬度指示
                String latitudeIndication = info.substring(10, 12);
                //纬度
                String hex_12_20= info.substring(12, 20);
                String latitude= ConvertData.hexToDegMinSeconds(hex_12_20);

                //速度
                long speed= ConvertData.hexToLong(info.substring(20, 24));
                //方向
                long direction= ConvertData.hexToLong(info.substring(24, 28));
                //定位状态
                int positionStatus=ConvertData.hexToInt(info.substring(28, 30));
                deviceTrackVO.longitudeIndication=longitudeIndication;
                deviceTrackVO.longitude=longitude;
                deviceTrackVO.latitudeIndication=latitudeIndication;
                deviceTrackVO.latitude=latitude;
                deviceTrackVO.speed=speed;
                deviceTrackVO.direction=direction;
                deviceTrackVO.positionStatus=positionStatus;
                //deviceTrackVO.insert();
                currentTrackVO=deviceTrackVO;
            }
        }
        return currentTrackVO;
    }
    /**
     * 返回实时天气
     * 0x58
     * Todo
     *
     * @param bdtxInfo
     */
    public static UserMessageVO cast0x58ToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头,直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        //温度，摄氏度
        int tmp = ConvertData.hexToInt(hexData.substring(0, 2));
        //天气实况代码
        int condCode = ConvertData.hexToInt(hexData.substring(2, 6));
        //风向
        int windDir = ConvertData.hexToInt(hexData.substring(6, 8));
        //风力
        int windSc = ConvertData.hexToInt(hexData.substring(8, 12));
        //风速，km/h
        int windSpd = ConvertData.hexToInt(hexData.substring(12, 16));
        //相对湿度
        int hum = ConvertData.hexToInt(hexData.substring(16, 18));
        //降雨量，mm
        int pcpn = ConvertData.hexToInt(hexData.substring(18, 22));
        //大气压强，单位：hPa:百帕
        int pres = ConvertData.hexToInt(hexData.substring(22, 26));
        //能见度，km
        int visable = ConvertData.hexToInt(hexData.substring(26, 28));
        //云量
        int cloud = ConvertData.hexToInt(hexData.substring(28, 32));
        //用户所在地区，范围在县级
        String adress = ConvertData.hexToString(hexData.substring(32), ConvertData.GB18030);
        //将数据转换成实体类
        RealTimeWeather realTimeWeather = new RealTimeWeather(tmp, condCode, windDir, windSc, windSpd, hum, pcpn, pres, visable, cloud, adress);
        Gson json = new Gson();
        String jsonStr = json.toJson(realTimeWeather);
        WeatherInfo weatherInfo = new WeatherInfo();
        weatherInfo.setIoType(Constant.RECEIVE_MESSAGE);
        weatherInfo.setJsonInfo(jsonStr);
        weatherInfo.setWeatherMode(WeatherModeType.RealTime.getValue());
        weatherInfo.setCreateTime(DateUtils.getSysDate());
        weatherInfo.insert();
        //sendMsg("onWeatherInfo");
        return null;
    }
    /**
     * 指令回应
     * 0x56
     * @return
     */
    public static UserMessageVO cast0x56ToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头,保留字段11位直接跳过不解析
        String hexData = bdtxInfo.content.substring(24);
        //消息标识
        String code = hexData.substring(0, 2);
        //消息指令
        String command = hexData.substring(2, 4);
        //时间戳(秒)
        long time = ConvertData.hexToLong(hexData.substring(4, 12));
        if("18".equals(code)){
            String weatherContent = ConvertData.hexToString(hexData.substring(12), ConvertData.GB18030);
            WeatherInfo weatherInfo = new WeatherInfo();
            weatherInfo.setIoType(Constant.RECEIVE_MESSAGE);
            weatherInfo.setJsonInfo(weatherContent);
            weatherInfo.setWeatherMode(WeatherModeType.Other.getValue());
            weatherInfo.setCreateTime(DateUtils.getSysDate());
            weatherInfo.insert();
            //sendMsg("onWeatherInfo");
        }
        return null;
    }
    /**
     * 返回天气预报
     * 0x59
     * Todo
     *
     * @param bdtxInfo
     */
    public static UserMessageVO cast0x59ToUserMessage(BDTXInfo bdtxInfo) {
        List<WeatherForecast> list = new ArrayList<>();
        //第1位请求头,直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        while (hexData.length() >= 42) {
            list.add(getForecast(hexData.substring(0, 42)));
            hexData = hexData.substring(42);
        }
        Gson json = new Gson();
        String jsonStr = json.toJson(list);
        WeatherInfo weatherInfo = new WeatherInfo();
        weatherInfo.setIoType(Constant.RECEIVE_MESSAGE);
        weatherInfo.setJsonInfo(jsonStr);
        weatherInfo.setWeatherMode(WeatherModeType.Forecast.getValue());
        weatherInfo.setCreateTime(DateUtils.getSysDate());
        //sendMsg("onWeatherInfo");
        return null;
    }



    /**
     * 返回灾害预警
     * 0x5A
     * Todo
     *
     * @param bdtxInfo
     */
    public static UserMessageVO cast0x5AToUserMessage(BDTXInfo bdtxInfo) {
        //第1位请求头,直接跳过不解析
        String hexData = bdtxInfo.content.substring(2);
        //灾害预警种类代码
        int type = ConvertData.hexToInt(hexData.substring(0, 2));
        //预警状态，文本，10个字节，不足位则填充0x00
        String status = ConvertData.hexToString(hexData.substring(2, 22), ConvertData.GB18030);
        //预警等级
        int level = ConvertData.hexToInt(hexData.substring(22, 24));
        //预警标题
        String title = ConvertData.hexToString(hexData.substring(24), ConvertData.GB18030);
        //将数据转换成实体类
        DisasterEarlyWarning disasterEarlyWarning = new DisasterEarlyWarning(type, status, level, title);
        Gson json = new Gson();
        String jsonStr = json.toJson(disasterEarlyWarning);
        WeatherInfo weatherInfo = new WeatherInfo();
        weatherInfo.setIoType(Constant.RECEIVE_MESSAGE);
        weatherInfo.setJsonInfo(jsonStr);
        weatherInfo.setWeatherMode(WeatherModeType.Warning.getValue());
        weatherInfo.setCreateTime(DateUtils.getSysDate());
        //sendMsg("onWeatherInfo");
        return null;
    }
    /**
     * 获取天气预报
     * Todo
     *
     * @param hex 16进制字符串，长度50
     */
    private static WeatherForecast getForecast(String hex) {
        //预报日期，1970年1月1日到现在的天数，拿到该值乘以86400000即可变成时间戳
        int day = ConvertData.hexToInt(hex.substring(0, 4));
        //最高温度
        int tmpMax = ConvertData.hexToInt(hex.substring(4, 6));
        //最低温度
        int tmpMin = ConvertData.hexToInt(hex.substring(6, 8));
        //白天天气情况状态代码
        int condCodeD = ConvertData.hexToInt(hex.substring(8, 12));
        //晚上天气情况状态代码
        int condCodeN = ConvertData.hexToInt(hex.substring(12, 16));
        //风向代码
        int windDir = ConvertData.hexToInt(hex.substring(16, 18));
        //风力
        int windSc = ConvertData.hexToInt(hex.substring(18, 22));
        //风速，km/h
        int windSpd = ConvertData.hexToInt(hex.substring(22, 26));
        //相对湿度
        int hum = ConvertData.hexToInt(hex.substring(26, 28));
        //降雨量，mm
        int pcpn = ConvertData.hexToInt(hex.substring(28, 32));
        //降雨概率
        int pop = ConvertData.hexToInt(hex.substring(32, 34));
        //大气压强，hPa
        int pres = ConvertData.hexToInt(hex.substring(34, 38));
        //紫外线指数
        int uvIndex = ConvertData.hexToInt(hex.substring(38, 40));
        //紫外线指数
        int vis = ConvertData.hexToInt(hex.substring(40, 42));
        return new WeatherForecast(day, tmpMax, tmpMin, condCodeD, condCodeN, windDir, windSc, windSpd, hum, pcpn, pop, pres, uvIndex, vis);
    }

    public static void main(String args[]) {
        try {
            //   String str="00A4120604012C1900100445763608084E20072107000001380145763608084E20072107000001380145763608084E20072107000001380145763608094E200721070000014801";
            //      cast0x00A4ToUserMessage(str,"545454");
            //    String lon="118°54'8.8″";
            //  double du=StringUtil.changeToDu(lon);
            //  System.out.println(du);
            //   double du=11841.5127;
            //  String lonStr=StringUtil.changeToDFM(du);
            //  System.out.println(lonStr);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
